package com.learn.springboot3.controller;

import com.learn.springboot3.config.MinioPropertiesConfig;
import com.learn.springboot3.mapper.UserMapper;
import com.learn.springboot3.pojo.User;
import com.learn.springboot3.service.UserService;
import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import org.apache.commons.codec.digest.Md5Crypt;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * ClassName: UserController
 * Package: com.learn.springboot3.controller
 * Description:
 *
 * @Author jiangcheng
 * @Create 2024/9/18 15:54
 * @Version 1.0
 */
@RestController
@RequestMapping("user")
public class UserController {

  @Autowired
    private UserService userService;



    @Autowired
    private UserMapper userMapper;

    @RequestMapping("list")
    public List<User> getUser(){



        return userMapper.getAll();
    }
    @Autowired
    private MinioClient minioClient;

    @Autowired
    private MinioPropertiesConfig minioPropertiesConfig;
    @PostMapping("/test")
    public String test(MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, ServerException, InsufficientDataException, ErrorResponseException, InvalidKeyException {

        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = dateFormat.format(date);
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        // 构建文件名，格式为 日期/UUID@原始文件名。
        // 例如：2024-09-01/123e4567-e89b-12d3-a456-426614174000@example.txt。
        String fileName = format+"/"+uuid+"@"+file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();
        ObjectWriteResponse objectWriteResponse = minioClient.putObject(
                PutObjectArgs.builder()
                        .bucket(minioPropertiesConfig.getBucketName())
                        .object(fileName)
                        .stream(inputStream, file.getSize(), -1)
                        .contentType(file.getContentType())
                        .build()
        );


        inputStream.close();
        return minioPropertiesConfig.getEndpoint()+"/"+
                minioPropertiesConfig.getBucketName()+"/"+fileName;
    }


}
